package com.carrie.mdg.myviewtext.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.carrie.mdg.myviewtext.R;
import com.carrie.mdg.myviewtext.adapter.MsgAdapter;
import com.carrie.mdg.myviewtext.entity.Msg;

import java.util.ArrayList;
import java.util.List;

import androidLib.activity.BaseActivity;

public class ChatActivity extends BaseActivity {
    private List<Msg> msgs = new ArrayList<>(10);
    private RecyclerView recyclerView;
    private EditText et_input;
    private Button send;
    private MsgAdapter adapter;

    public static void actionStart(Context context) {
        Intent intent = new Intent(context, ChatActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initVariables() {
        initMsgs();
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        setContentView(R.layout.activity_chat);
        et_input = (EditText) findViewById(R.id.ed_input);
        send = (Button) findViewById(R.id.btn_send);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerview_msgs);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        adapter = new MsgAdapter(msgs);
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void initEvent() {
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String msg = et_input.getText().toString().trim();
                if (!TextUtils.isEmpty(msg)) {
                    Msg msg1 = new Msg(msg, Msg.TYPE_SEND);
                    msgs.add(msg1);
                    adapter.notifyItemInserted(msgs.size() - 1);//当有新消息时，刷新Recyclerview中的显示
                    recyclerView.scrollToPosition(msgs.size() - 1);//讲Recyclerview定位到最后一行
                    et_input.setText("");
                }
            }
        });
    }

    @Override
    protected void loadData() {

    }

    private void initMsgs() {
        Msg msg1 = new Msg("Hello guy", Msg.TYPE_RECEIVED);
        msgs.add(msg1);
        Msg msg2 = new Msg("Who is that?", Msg.TYPE_SEND);
        msgs.add(msg2);
        Msg msg3 = new Msg("This is tom, nice talking to you.", Msg.TYPE_RECEIVED);
        msgs.add(msg3);
        Msg msg4 = new Msg("Hello.", Msg.TYPE_SEND);
        msgs.add(msg4);
        Msg msg5 = new Msg("What are you doing now", Msg.TYPE_RECEIVED);
        msgs.add(msg5);
        Msg msg6 = new Msg("I am reading a book.", Msg.TYPE_SEND);
        msgs.add(msg6);
        Msg msg7 = new Msg("What kind book?", Msg.TYPE_RECEIVED);
        msgs.add(msg7);
        Msg msg8 = new Msg("It talks about megic", Msg.TYPE_SEND);
        msgs.add(msg8);
        Msg msg9 = new Msg("oh, cool", Msg.TYPE_RECEIVED);
        msgs.add(msg9);
    }
}
